package Font;

import Dao.Impl.PetStoreImpl;
import Entity.account;
import Entity.pet;
import Entity.pet_store;
import Service.AccountServiceImpl;
import Service.PetOwnerServiceImpl;
import Service.PetServiceImpl;
import Service.PetStoreServiceImpl;

import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;
import java.util.Scanner;

public class login {
    public static void login() throws SQLException, ParseException {
        PetServiceImpl petService =new PetServiceImpl();
        PetOwnerServiceImpl petOwnerService = new PetOwnerServiceImpl();
        PetStoreServiceImpl petStoreService = new PetStoreServiceImpl();
        AccountServiceImpl accountService = new AccountServiceImpl();
        PetStoreImpl petStore = new PetStoreImpl();

        System.out.println("请选择输入登陆模式");
        System.out.println("输入1为宠物主人登录，输入2为宠物商店登录，输入其他数字为退出");
        boolean a=true;
        Scanner scanner = new Scanner(System.in);
        String i=scanner.nextLine();
        Boolean flag = true;
        int id=0;
        while (flag)
        {
            if(Integer.parseInt(i)==1)
            {
                id= petOwnerService.login();
                System.out.println("登陆成功，你可以购买和卖出宠物，选择1为购买宠物，选择2为卖出宠物");
                System.out.println("1:购买宠物");
                System.out.println("2:卖出宠物");
                int flagg=Integer.parseInt(scanner.nextLine());
                if(flagg==1)
                {
                    int pet_id=petOwnerService.buy();
                    accountService.insert(id,pet_id);
                }
                else if(flagg==2)
                {
                    //卖出宠物
                    List<pet> pets =petOwnerService.sell(id);
                    System.out.println("******************我的宠物列表****************");
                    int flag1=0;
                    for(pet p:pets)
                    {
                        flag1++;
                        System.out.println("序列号为:"+flag1+"     我的名字为:"+p.getName()+"    我是:"+p.getTypename()+"      要购买我要花:"+p.getPrice()+"个元宝");
                    }

                    System.out.println("---------请选择你要出售的宠物，并输入选择项的序号---------");
                    String s=scanner.nextLine();
                    int flag2=Integer.parseInt(s);
                    int pet_id=pets.get(flag2-1).getId();
                    System.out.println("---------你要出售的宠物信息如下---------");
                    System.out.println("宠物名字："+pets.get(flag2-1).getName());
                    System.out.println("宠物类别："+pets.get(flag2-1).getTypename());

                    System.out.println("---------下面是现有的宠物商店，请你选择要卖的商店序号---------");
                    petStoreService.GetAllPetStore();
                    int aa=Integer.parseInt(scanner.nextLine());
                    System.out.println("---------请你确认是否要卖出，y代表卖出，n代表不卖---------");
                    String b=scanner.nextLine();
                    if(b.equals("y"))
                    {
                        accountService.insert(id,pet_id,petStoreService.find(aa));
                    }
                    else if(b.equals("n"))
                    {
                        System.out.println("对不起，我反悔了，我不卖了，我发现这只宠物我还是要养的");
                    }

                }
            }


            else if(Integer.parseInt(i)==2)
            {
                id= petStoreService.login();
                System.out.println("登陆成功，你可以培育和查看宠物和查看账单，选择1为培育，选择2为查看宠物，选择3为查看账单");
                System.out.println("1:培育宠物");
                System.out.println("2:查看宠物");
                System.out.println("3:查看账单");

                int flagg=Integer.parseInt(scanner.nextLine());
                if(flagg==1)
                {
                    System.out.println("输入宠物名字");
                    String name=scanner.nextLine();
                    System.out.println("输入宠物类型");
                    String type=scanner.nextLine();
                    System.out.println("输入宠物价格");
                    String price=scanner.nextLine();
                    System.out.println("输入宠物爱心指数 0-5表示爱心值");
                    String love=scanner.nextLine();
                    System.out.println("输入宠物健康值 0为不健康，1为健康");
                    String health=scanner.nextLine();
                    pet pet= new pet();
                    pet.setId(id);
                    pet.setName(name);
                    pet.setTypename(type);
                    pet.setPrice(Integer.parseInt(price));
                    pet.setLove(Integer.parseInt(love));
                    pet.setHealth(Integer.parseInt(health));
                    petService.add(pet);
                }
                else if (flagg==2)
                {
                   List<pet> pets= petStoreService.AllPetBystore(id);
                    int flag1=0;
                   for (pet p:pets)
                   {
                       flag1++;
                       System.out.println("序列号为:"+flag1+"     我的名字为:"+p.getName()+"    我是:"+p.getTypename()+"      要购买我要花:"+p.getPrice()+"个元宝");
                   }
                }
                else if (flagg==3)
                {
                     List<account> accounts=accountService.GetAccountbystore(id);
                    int flag1=0;
                    for (account account:accounts)
                    {
                        flag1++;
                        System.out.println("序列号为:"+flag1+"     账单id为:"+account.getId()+"    购买人id:"+account.getSeller_id()+"      账单购买价格:"+account.getPrice()+"个元宝");
                    }
                }

            }
            else
            {
                // TODO
                flag=false;
            }
            System.out.println("请选择输入登陆模式");
            System.out.println("输入1为宠物主人登录，输入2为宠物商店登录，输入其他数字为退出");
            i=scanner.nextLine();
        }


    }
}
